package com.work.biz.service.impl;

import java.util.List;

import com.work.biz.domain.BizStudent;
import com.work.biz.mapper.BizClazzMapper;
import com.work.biz.mapper.BizNoticeDetailMapper;
import com.work.biz.mapper.BizStudentMapper;
import com.work.common.utils.DateUtils;
import com.work.sys.domain.SysUser;
import com.work.sys.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.stream.Collectors;

import com.work.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.work.biz.domain.BizNoticeDetail;
import com.work.biz.mapper.BizNoticeMapper;
import com.work.biz.domain.BizNotice;
import com.work.biz.service.IBizNoticeService;

/**
 * 通知信息Service业务层处理
 *
 * @author lee
 * @date 2021-04-22
 */
@Service
public class BizNoticeServiceImpl implements IBizNoticeService {
    @Autowired
    private BizNoticeMapper bizNoticeMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private BizStudentMapper studentMapper;
    @Autowired
    private BizNoticeDetailMapper detailMapper;

    /**
     * 查询通知信息
     *
     * @param noticeId 通知信息ID
     * @return 通知信息
     */
    @Override
    public BizNotice selectBizNoticeById(Long noticeId) {
        return bizNoticeMapper.selectBizNoticeById(noticeId);
    }

    /**
     * 查询通知信息列表
     *
     * @param bizNotice 通知信息
     * @return 通知信息
     */
    @Override
    public List<BizNotice> selectBizNoticeList(BizNotice bizNotice) {
        return bizNoticeMapper.selectBizNoticeList(bizNotice);
    }

    /**
     * 新增通知信息
     *
     * @param bizNotice 通知信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBizNotice(BizNotice bizNotice) {
        bizNotice.setCreateTime(DateUtils.getNowDate());
        SysUser user = userMapper.selectSysUserById(bizNotice.getCreatorId());
        bizNotice.setCreatorName(user.getNickName());
        int rows = bizNoticeMapper.insertBizNotice(bizNotice);
        insertBizNoticeDetail(bizNotice);
        return rows;
    }

    /**
     * 修改通知信息
     *
     * @param bizNotice 通知信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBizNotice(BizNotice bizNotice) {
        bizNotice.setUpdateTime(DateUtils.getNowDate());
        bizNotice.setNoticeType("1");
        //insertBizNoticeDetail(bizNotice);
        BizNoticeDetail detail = new BizNoticeDetail();
        detail.setNoticeId(bizNotice.getNoticeId());
        List<BizNoticeDetail> bizNoticeDetailList = detailMapper.selectBizNoticeDetailList(detail);
        bizNoticeDetailList.forEach(bizNoticeDetail -> {
            bizNoticeDetail.setReadStatus("0");
            bizNoticeDetail.setUpdateTime(bizNotice.getUpdateTime());
            bizNoticeDetail.setNoticeTitle(bizNotice.getNoticeTitle());
            bizNoticeDetail.setNoticeContent(bizNotice.getNoticeContent());
            detailMapper.updateBizNoticeDetail(bizNoticeDetail);
        });
        return bizNoticeMapper.updateBizNotice(bizNotice);
    }

    /**
     * 批量删除通知信息
     *
     * @param noticeIds 需要删除的通知信息ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBizNoticeByIds(Long[] noticeIds) {
        bizNoticeMapper.deleteBizNoticeDetailByNoticeIds(noticeIds);
        return bizNoticeMapper.deleteBizNoticeByIds(noticeIds);
    }

    /**
     * 删除通知信息信息
     *
     * @param noticeId 通知信息ID
     * @return 结果
     */
    @Override
    public int deleteBizNoticeById(Long noticeId) {
        bizNoticeMapper.deleteBizNoticeDetailByNoticeId(noticeId);
        return bizNoticeMapper.deleteBizNoticeById(noticeId);
    }

    /**
     * 新增通知信息详情信息
     *
     * @param bizNotice 通知信息对象
     */
    public void insertBizNoticeDetail(BizNotice bizNotice) {
        Long noticeId = bizNotice.getNoticeId();
        if("1".equals(bizNotice.getNoticeType())) {
            List<BizStudent> students = studentMapper.selectBizStudentByClazz(bizNotice.getClazzIds());
            students.forEach(s -> {
                BizNoticeDetail detail = new BizNoticeDetail();
                detail.setStudentId(s.getStudentId());
                detail.setStudentName(s.getStudentName());
                detail.setStudentNo(s.getStudentNo());
                detail.setNoticeId(noticeId);
                detail.setNoticeContent(bizNotice.getNoticeContent());
                detail.setCreateBy(bizNotice.getCreatorName());
                detail.setCreateTime(bizNotice.getCreateTime());
                detail.setNoticeTitle(bizNotice.getNoticeTitle());
                detailMapper.insertBizNoticeDetail(detail);
            });
        }else if("2".equals(bizNotice.getNoticeType())) {
            List<BizNoticeDetail> bizNoticeDetailList = bizNotice.getDetails();
            if (StringUtils.isNotNull(bizNoticeDetailList)) {
                for (BizNoticeDetail bizNoticeDetail : bizNoticeDetailList) {
                    if(StringUtils.isNull(bizNoticeDetail.getStudentId())) {
                        bizNoticeDetail.setStudentId(studentMapper.selectBizStudentByNo(bizNoticeDetail.getStudentNo()).getStudentId());
                    }
                    bizNoticeDetail.setNoticeId(noticeId);
                    bizNoticeDetail.setCreateBy(bizNotice.getCreatorName());
                    bizNoticeDetail.setCreateTime(bizNotice.getCreateTime());
                    bizNoticeDetail.setNoticeTitle(bizNotice.getNoticeTitle());
                    detailMapper.insertBizNoticeDetail(bizNoticeDetail);
                }
            }
        }else {
            BizNoticeDetail detail = new BizNoticeDetail();
            detail.setNoticeId(noticeId);
            List<BizNoticeDetail> bizNoticeDetailList = detailMapper.selectBizNoticeDetailList(detail);
            bizNoticeMapper.deleteBizNoticeDetailByNoticeId(bizNotice.getNoticeId());
            if (StringUtils.isNotNull(bizNoticeDetailList)) {
                for (BizNoticeDetail bizNoticeDetail : bizNoticeDetailList) {
                    if(StringUtils.isNull(bizNoticeDetail.getStudentId())) {
                        bizNoticeDetail.setStudentId(studentMapper.selectBizStudentByNo(bizNoticeDetail.getStudentNo()).getStudentId());
                    }
                    bizNoticeDetail.setNoticeId(noticeId);
                    bizNoticeDetail.setCreateBy(bizNotice.getCreatorName());
                    bizNoticeDetail.setCreateTime(bizNotice.getUpdateTime());
                    bizNoticeDetail.setNoticeTitle(bizNotice.getNoticeTitle());
                    bizNoticeDetail.setNoticeContent(bizNotice.getNoticeContent());
                    detailMapper.insertBizNoticeDetail(bizNoticeDetail);
                }
            }
        }
    }
}
